using Aliphant.Mvvm.XEvent.Interface;
using Aliphant.Mvvm.XEvent.Ioc;
using Aliphant.Mvvm.XEvent.Models;
using Aliphant.Mvvm.XEvent.UI;
using GrpcClient.Managers;
using GrpcClient.Models;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Windows.Input;

namespace GrpcClient.ViewModels
{
    public class WebAppControlViewModel : BindableBase, INavigate
    {

        private int _selected;
        public int Selected { get => _selected; set { SetProperty(ref _selected, value); } }


        public ObservableCollection<NavigateWebModel> AppCollection { get; private set; } = new ObservableCollection<NavigateWebModel>();

        public ICommand NavigationCommand
        {
            get => new RelayCommand<object>(obj =>
            {
                if (obj is NavigateWebModel webNavi)
                {
                    //NavigateParameter nparam = new NavigateParameter();
                    //nparam.values.Add("uri", webNavi.WebUri);
                    //XContainerLocator.Instance.NavigateTo("body", webNavi.ViewName, nparam);

                    XContainerLocator.Instance.NavigateTo("body", "browser");
                    TabManager.Instance.Create(webNavi.WebUri);
                    TabManager.Instance.UpdateInputUrl(webNavi.WebUri);
                }
            });
        }

        public void OnNavigateTo(NavigateParameter navigateParameter)
        {
            Init();
        }

        private void Init()
        {
            AppCollection.Clear();
            var models = ConfigContainer.Instance.cacheManager.Get<List<NavigateWebModel>>("apps");

            if (models != null)
            {
                foreach (var item in models)
                {
                    AppCollection.Add(item);
                }
            }
        }

        public ICommand DeleteCollectionCommand
        {
            get => new RelayCommand<object>(obj =>
            {
                var list = AppCollection.ToList();
                list.RemoveAt(Selected);
                ConfigContainer.Instance.cacheManager.Update("apps", list);

                Init();
            });
        }

    }
}
